﻿using SalaryManagementSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SalaryManagementSystem.Controllers
{
    public class AdminController : Controller
    {
        private SalaryManagementBaseEntities1 db = new SalaryManagementBaseEntities1();
        // GET: Admin
        public ActionResult Index()
        {
            if(Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            ViewBag.username = Session["username"];
            
            return View();
        }
        public ActionResult Update()
        {
            //对于未使用HttpPost传值的跳转
            //将会判断用户信息，如果为空将强制跳转登录页面
            if (Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            return View();
        }

        //重载修改方法，对HttpPost传值的跳转执行修改业务
        [HttpPost]
        public ActionResult Update(int userid, string username, string usersex, string useraccount, string userpwd)
        {
            //过滤空格
            username = username.Trim();
            usersex = usersex.Trim();
            useraccount = useraccount.Trim();
            userpwd = userpwd.Trim();
            AdminTable at = db.AdminTable.FirstOrDefault(p => p.id == userid);
            if (username.Length == 0 || usersex.Length == 0 || useraccount.Length == 0 || userpwd.Length == 0)
            {
                ViewBag.notice = "修改不能为空!";
                return View();
            }
            if (username == Session["username"].ToString() && usersex == Session["usersex"].ToString() && useraccount == Session["useraccount"].ToString() && userpwd == Session["userpwd"].ToString())
            {
                ViewBag.notice = "请修改后再提交!";
                return View();
            }
            if (at != null)
            {
                at.account = useraccount;
                at.stafferName = username;
                at.accountpwd = userpwd;
                at.sex = usersex;
                db.Entry(at).State = System.Data.Entity.EntityState.Modified;
                if (db.SaveChanges() > 0)
                {
                    ViewBag.notice = "信息修改成功!";
                    //更新临时信息
                    Session["username"] = username;
                    Session["useraccount"] = useraccount;
                    Session["usersex"] = usersex;
                    Session["userpwd"] = userpwd;
                    ViewBag.useraccount = Session["useraccount"];
                    ViewBag.userpwd = Session["userpwd"];
                    ViewBag.username = Session["username"];
                    ViewBag.usersex = Session["usersex"];
                }
            }
            else
            {
                ViewBag.notice = "未获取到数据，请重试！";
            }
            return View();
        }
        public ActionResult StafferEdit()
        {
            //判断用户信息，如果为空将强制跳转登录页面
            if (Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            //获取路由传值的数据
            string id = Request["id"] as string;
            if(id!=null)
            {
                int num = int.Parse(id);
                StafferTable st = db.StafferTable.FirstOrDefault(p => p.id == num);
                db.Entry(st).State = System.Data.Entity.EntityState.Deleted;
                if (db.SaveChanges() > 0)
                {
                    ViewBag.notice = "删除成功!";
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult StafferEdit(string username, string usersex, string useraccount, string userpwd, string userjob, DateTime usergetjobtime, string usersalary, string staffertype, string note)
        {
            //为了避免用户输入int类型工资为空导致程序报错
            //在表单中设置为string类型，收到值后再转换为int类型存入数据库
            int salary = int.Parse(usersalary);
            StafferTable sta = new StafferTable()
            {
                stafferName = username,
                sex = usersex,
                account = useraccount,
                accountpwd = userpwd,
                job = userjob,
                getJobTime = usergetjobtime,
                salary = salary,
                updateTime = DateTime.Now,
                stafferType = staffertype,
                note = note
            };
            db.Entry(sta).State = System.Data.Entity.EntityState.Added;
            if (db.SaveChanges() > 0)
            {
                ViewBag.notice = "添加成功";
                return View();
            }
            return View();
        }
        public ActionResult Edit()
        {
            if (Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            
            if (RouteData.Values["id"] != null)
            {
                int id = int.Parse(RouteData.Values["id"].ToString());
                StafferTable st = db.StafferTable.FirstOrDefault(p => p.id == id);
                ViewBag.id = st.id;
                ViewBag.stafferName = st.stafferName;
                ViewBag.sex = st.sex;
                ViewBag.account = st.account;
                ViewBag.accountpwd = st.accountpwd;
                ViewBag.job = st.job;
                ViewBag.getJobTime = st.getJobTime;
                ViewBag.salary = st.salary;
                ViewBag.stafferType = st.stafferType;
                ViewBag.note = st.note;
            }
            return View();
        }
        
        [HttpPost]
        public ActionResult doEdit(int userid, string username, string usersex, string useraccount, string userpwd, string userjob, DateTime usergetjobtime, string usersalary, string staffertype, string note)
        {
            if (username.Length == 0 || usersex.Length == 0 || useraccount.Length == 0 || userpwd.Length == 0 || userjob.Length ==0 || usergetjobtime == null || usersalary.Length == 0 || staffertype.Length ==0)
            {
                ViewBag.notice = "修改不能为空!";
                return RedirectToRoute(new { controller = "Admin", action = "Edit" });
            }
            int salary = int.Parse(usersalary);
            
            StafferTable st = db.StafferTable.FirstOrDefault(p => p.id == userid);
            st.stafferName = username;
            st.sex = usersex;
            st.account = useraccount;
            st.accountpwd = userpwd;
            st.job = userjob;
            st.getJobTime = usergetjobtime;
            st.salary = salary;
            st.updateTime = DateTime.Now;
            st.stafferType = staffertype;
            if(note.Length != 0)
            {
                st.note = note;
            }
            else
            {
                st.note = "——";
            }
            if (db.SaveChanges() > 0)
            {
                ViewBag.notice = "删除成功!";
            }
            return RedirectToRoute(new { controller = "Admin", action = "StafferEdit" });
        }

    }
}